/********************************************************/
/*	Copyright (C) 2016 Gong Li Bin			*/
/*	Project:	GlbCloud			*/
/*	Author:		gong_libin			*/
/*	Date:		2012_01_09			*/
/*	File:		MrMap.h				*/
/********************************************************/

#ifndef _MRMAP_H
#define	_MRMAP_H

#include "MrGlobal.h"
#include <iostream>
#include <string>
#include <map>

typedef struct tagMrMapDigit
{
	bool operator()(const ULONGLONG ullKey1, const ULONGLONG ullKey2) const {
		return static_cast<int>(ullKey1 - ullKey2) < 0;
	}
}MRMAPDIGIT_O;

typedef struct tagMrMapString
{
	bool operator()(const std::string sKey1, const std::string sKey2) const {
		return sKey1 < sKey2;
	}
}MRMAPSTRING_O;

template<typename K, typename V, typename C>
class CMrMap
{
public:
	typedef typename std::map<K, V, C>		MR_MAP;
	typedef typename std::map<K, V, C>::iterator	MR_ITE;

	CMrMap() {};
	virtual ~CMrMap() {};

	ULONG MrMapSize();
	bool MrMapMrpty();
	MR_ITE MrMapEnd();
	MR_ITE MrMapBegin();
	void MrMapDestroy();
	void MrMapDisplay();
	void MrMapTraversal(int (*pFunction)(void*));

	inline void MrMapDelete(K);
	inline bool MrMapInsert(K, V&);
	inline bool MrMapUpdate(K, V&);
	inline bool MrMapSelect(K, V&, int);

private:
	MR_MAP m_CMap;

};

template<typename K, typename V, typename C>
ULONG CMrMap<K, V, C>::MrMapSize()
{
	return (ULONG)m_CMap.size();
}

template<typename K, typename V, typename C>
bool CMrMap<K, V, C>::MrMapMrpty()
{
	return m_CMap.empty();
}

template<typename K, typename V, typename C>
typename std::map<K, V, C>::iterator CMrMap<K, V, C>::MrMapEnd()
{
	return m_CMap.end();
}

template<typename K, typename V, typename C>
typename std::map<K, V, C>::iterator CMrMap<K, V, C>::MrMapBegin()
{
	return m_CMap.begin();
}

template<typename K, typename V, typename C>
void CMrMap<K, V, C>::MrMapDestroy()
{
	MR_ITE iteCur = MrMapBegin();

	for (; iteCur != MrMapEnd(); iteCur ++) {
		free(iteCur->second);
	}

	return;
}

template<typename K, typename V, typename C>
void CMrMap<K, V, C>::MrMapDisplay()
{
	int iCount = 0;
	MR_ITE iteCur = MrMapBegin();

	for (; iteCur != MrMapEnd(); iteCur ++) {
		MR_PRINT("%04d: ", iCount ++);
		std::cout << "{ " << iteCur->first << ":\t" << iteCur->second << " }" << std::endl;
	}

	return;
}

template<typename K, typename V, typename C>
void CMrMap<K, V, C>::MrMapTraversal(int (*pFunction)(void*))
{
	MR_ITE iteCur = MrMapBegin();

	for (; iteCur != MrMapEnd(); iteCur ++) {
		(*pFunction)((void*)&iteCur->second);
	}

	return;
}

template<typename K, typename V, typename C>
inline void CMrMap<K, V, C>::MrMapDelete(K Key)
{
	m_CMap.erase(Key);

	return;
}

template<typename K, typename V, typename C>
inline bool CMrMap<K, V, C>::MrMapInsert(K Key, V& Value)
{
	std::pair<MR_ITE, bool> pairReturn;
	std::pair<K, V> pairInsert(Key, Value);

	pairReturn = m_CMap.insert(pairInsert);

	return pairReturn.second;
}

template<typename K, typename V, typename C>
inline bool CMrMap<K, V, C>::MrMapUpdate(K Key, V& Value)
{
	MrMapDelete(Key);

	return MrMapInsert(Key, Value);
}

template<typename K, typename V, typename C>
inline bool CMrMap<K, V, C>::MrMapSelect(K Key, V& Value, int iSize)
{
	MR_ITE iteCur = MrMapBegin();

	iteCur = m_CMap.find(Key);
	if (iteCur != MrMapEnd()) {
		memcpy(static_cast<void*>(&Value), (void*)&(iteCur->second), iSize);
	}
	else {
		return false;
	}

	return true;
}

#endif /* _MRMAP_H */
